package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.Book;
import com.example.demo.vo.BookVO;
import com.example.demo.vo.PreviewVO;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Shanks
 * @date 2020-10-22
 */
@Slf4j
@Api(value = "BookController", tags = {"书籍"})
@RestController
public class BookController {

    @Resource
    private BookMapper bookMapper;

    @ApiOperation(value = "查询书籍列表", notes = "查询书籍列表")
    @GetMapping("/books")
    public R<List<BookVO>> list() {
        List<Book> books = bookMapper.selectList(null);
        List<BookVO> bookVoList = Lists.newArrayList();
        for (Book book : books) {
            BookVO vo = new BookVO();
            vo.setId(book.getId());
            vo.setCode(book.getCode());
            vo.setName(book.getName());
            vo.setAuthor(book.getAuthor());
            String preview = book.getPreview();
            vo.setPreview(JSON.parseObject(preview, new TypeReference<PreviewVO>() {}));
            vo.setWordTotal(book.getWordTotal());
            vo.setPrintTotal(book.getPrintTotal());
            vo.setSaleTotal(book.getSaleTotal());
            vo.setWriteDate(book.getWriteDate());
            vo.setSaleOnlineTime(book.getSaleOnlineTime());
            vo.setCreateBy(book.getCreateBy());
            vo.setCreateTime(book.getCreateTime());
            vo.setUpdateBy(book.getUpdateBy());
            vo.setUpdateTime(book.getUpdateTime());
            bookVoList.add(vo);
        }
        return R.ok(bookVoList);
    }

}